﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;

namespace ConsoleApp
{
    /// <summary>
    /// 同步调用
    /// </summary>
    public class SyncTest
    {
        public static void Show()
        {
            Console.WriteLine("Program类中的Main()函数的线程ID是{0}", Thread.CurrentThread.ManagedThreadId);//Environment.CurrentManagedThreadId
            if (Thread.CurrentThread.IsThreadPoolThread)//判断当前线程是否托管在线程池上
            {
                Console.WriteLine("Program类中的Main()函数的线程托管于线程池");
            }
            else
            {
                Console.WriteLine("Program类中的Main()函数的线程没有托管在线程池上");
            }
            Console.WriteLine();

            SyncTest.Method();//调用静态类的静态方法 可知使用同一个线程，也是非托管线程池上

        }


        public static void Method()
        {

            Console.WriteLine("SyncTest类中的Method()函数的线程ID是{0}", Thread.CurrentThread.ManagedThreadId);//Environment.CurrentManagedThreadId
            if (Thread.CurrentThread.IsThreadPoolThread)//判断当前线程是否托管在线程池上
            {
                Console.WriteLine("SyncTest类中的Method()函数的线程托管于线程池");
            }
            else
            {
                Console.WriteLine("SyncTest类中的Method()函数的线程没有托管在线程池上");
            }
        }
    }


    /// <summary>
    /// 异步调用
    /// 
    /// </summary>
    public class AsyncTest
    {
        public delegate long AsyncHandler();//委托的声明方式类似于函数，只是比函数多了一个delegate关键字
        public static void Show()
        {
            Console.WriteLine("Program类中的Main()函数的线程ID是{0}", Thread.CurrentThread.ManagedThreadId);//Environment.CurrentManagedThreadId
            if (Thread.CurrentThread.IsThreadPoolThread)//判断当前线程是否托管在线程池上
            {
                Console.WriteLine("Program类中的Main()函数的线程托管于线程池");
            }
            else
            {
                Console.WriteLine("Program类中的Main()函数的线程没有托管在线程池上");
            }
            Console.WriteLine();

            //把Method 方法分配给委托对象
            AsyncHandler async = new AsyncHandler(AsyncTest.Method); //

            //发起一个异步调用的方法,返回IAsyncResult 对象
            //IAsyncResult result = async.BeginInvoke(null, null);
            var result = async.BeginInvoke(new AsyncCallback(Callback), null);


            //这里会阻碍线程,直到方法执行完毕
            async.EndInvoke(result);
        }

        public static void Callback(IAsyncResult iar)
        {
            AsyncResult ar = (AsyncResult)iar;
            AsyncHandler del = (AsyncHandler)ar.AsyncDelegate;
            //回调方法中调用了EndInvoke
            long result = del.EndInvoke(ar);//得到结果了
            //return result;
        }

        public static long Method()
        {
            Console.WriteLine("AsyncTest类中的Method()函数的线程ID是{0}", Thread.CurrentThread.ManagedThreadId);//Environment.CurrentManagedThreadId
            if (Thread.CurrentThread.IsThreadPoolThread)//判断当前线程是否托管在线程池上
            {
                Console.WriteLine("AsyncTest类中的Method()函数的线程托管于线程池");
            }
            else
            {
                Console.WriteLine("AsyncTest类中的Method()函数的线程没有托管在线程池上");
            }
            return Thread.CurrentThread.ManagedThreadId;
        }
    }



}
